package com.store;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.jedi.BaseObj;
import com.tss.util.DbConn;
import com.tss.util.DbRs;

public class CompanyMgr extends BaseObj{
	public List get(String otherSQL,String orderField) {
		clearErr();
		DbConn conn = new DbConn();
		DbRs rs = null;
		try {
			String sql = "";
			sql = "select * from companyinfo where 1=1";
			if (otherSQL != null && !otherSQL.trim().equals("")) 
				sql += otherSQL;
			if (orderField != null && !orderField.trim().equals(""))
				sql += " order by " + orderField;
			if (getTopNum() > 0)
				sql += " limit " + getTopNum();
			conn.prepare(sql);
			rs = conn.executeQuery();
		}
		catch(Exception ex) {
			ex.printStackTrace();
			setErr(ex.getMessage());
			return null;
		}
		finally {
			conn.close();
		}
		if (rs != null && rs.size() >0) {
			List r = new ArrayList();
			for(int i=0;i<rs.size();i++) {
				Company company = new Company(get(rs,i,"comid"));
				company.setComid(getInt(rs,i,"comid"));
				company.setComman(get(rs,i,"comman"));
				company.setCommanmail(get(rs,i,"commanmail"));
				company.setCommantel(get(rs,i,"commantel"));
				company.setComtel(get(rs,i,"comtel"));
				company.setComnam(get(rs,i,"comname"));
				r.add(company);
			}
			return r;
		}
		return null;
	}
	
	public List get(String otherSQL,String orderField,int pageNo, int pageSize) {
		clearErr();
		DbRs rs = null;
		DbConn conn = null;
		try {
			conn = new DbConn();
			String sqlCount = "select count(*) as total from companyinfo where 1=1";
			if (otherSQL != null && !otherSQL.trim().equals(""))
				sqlCount += otherSQL;
			conn.prepare(sqlCount);
			DbRs rsCount = conn.executeQuery();
			int total = getInt(rsCount,0,"total");
			setTotal(total);
			boolean b = total % pageSize == 0;
			int pageCount = b ? (total / pageSize) : (total / pageSize + 1);
			if (pageNo > pageCount) pageNo = pageCount;
			String sql = "select * ,substring(createtime, 1,10) as screatetime from companyinfo where 1=1";
			if (otherSQL != null && !otherSQL.trim().equals(""))
				sql += otherSQL;
			if (orderField != null && !orderField.trim().equals(""))
				sql += " order by " + orderField;
			conn.prepare(sql);
			rs = conn.executeQuery(pageNo, pageSize);
		} catch (SQLException ex) {
			ex.printStackTrace();
			setErr(ex.getMessage());
			return new ArrayList();
		} catch (Exception e) {
			e.printStackTrace();
			setErr(e.getMessage());
			return new ArrayList();
		} finally {
			conn.close();
		}
		List r = new ArrayList();
		if (rs == null || rs.size() <= 0)
			return r;
		for (int i = 0; i < rs.size(); i++) {
			Company company = new Company(get(rs,i,"comid"));
			company.setComid(getInt(rs,i,"comid"));
			company.setComman(get(rs,i,"comman"));
			company.setCommanmail(get(rs,i,"commanmail"));
			company.setCommantel(get(rs,i,"commantel"));
			company.setComtel(get(rs,i,"comtel"));
			company.setComnam(get(rs,i,"comname"));
			company.setComaddress(get(rs,i,"comaddress"));
			company.setCreatetime(get(rs,i,"screatetime"));
			
			r.add(company);
		}
		return r;
	}
	public Company getComInfo(int comid){
		
		
		clearErr();
		Company company = new Company();
		DbConn conn = new DbConn();
		try {
			String sql = "";
			sql = "select *, substring(createtime, 1, 10) as createtime from companyinfo where comid = ?";
			conn.prepare(sql);
			conn.setInt(1,comid);
			DbRs rs = conn.executeQuery();
			if (rs == null || rs.size() ==0) return null;
			company.setComid(getInt(rs,0,"comid"));
			company.setComman(get(rs,0,"comman"));
			company.setCommanmail(get(rs,0,"commanmail"));
			company.setCommantel(get(rs,0,"commantel"));
			company.setComnam(get(rs,0,"comname"));
			company.setComtel(get(rs,0,"comtel"));
			company.setCreatetime(get(rs,0,"createtime"));
			company.setComremark(get(rs,0,"comremark"));
			company.setComaddress(get(rs,0,"comaddress"));
			return company;
		}
		catch(Exception ex) {
			ex.printStackTrace();
			setErr(ex.getMessage());
			return null;
		}
		finally {
			conn.close();
		}
	}
	/**
	 * 厂商下拉菜单
	 * @return
	 */
	public List getComGroupBycomid() {
		clearErr();
		DbConn conn = new DbConn();
		DbRs rs = null;
		try {
			String sql = "";
			sql = "select comid,comname from companyinfo where 1=1 group by comid,comname ";
			
			conn.prepare(sql);
			rs = conn.executeQuery();
		}
		catch(Exception ex) {
			ex.printStackTrace();
			setErr(ex.getMessage());
			return null;
		}
		finally {
			conn.close();
		}
		if (rs != null && rs.size() >0) {
			List r = new ArrayList();
			for(int i=0;i<rs.size();i++) {
				Company company = new Company(get(rs,i,"comid"));
				company.setComid(getInt(rs,i,"comid"));
				company.setComnam(get(rs,i,"comname"));
			
				
				
				r.add(company);
			}
			return r;
		}
		return null;
	}
	public List getCompanys(){
	
		
			clearErr();
			DbConn conn = new DbConn();
			DbRs rs = null;
			try {
				String sql = "";
				sql = "select * from companyinfo where 1=1";
				
				conn.prepare(sql);
				rs = conn.executeQuery();
			}
			catch(Exception ex) {
				ex.printStackTrace();
				setErr(ex.getMessage());
				return null;
			}
			finally {
				conn.close();
			}
			if (rs != null && rs.size() >0) {
				List r = new ArrayList();
				for(int i=0;i<rs.size();i++) {
					Company company = new Company(get(rs,i,"comid"));
					company.setComid(getInt(rs,i,"comid"));
					company.setComman(get(rs,i,"comman"));
					company.setCommanmail(get(rs,i,"commanmail"));
					//project(get(rs,i,"phone"));
					company.setCommantel(get(rs,i,"commantel"));
					company.setComtel(get(rs,i,"comtel"));
					company.setComnam(get(rs,i,"comname"));
					company.setCreatetime(get(rs,i,"createtime"));
					
					
					r.add(company);
				}
				return r;
			}
			return null;
		}
}
